package com.yx.cloud.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.iot.base.service.req.FeedBackParam;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.FeedbackmessageActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.iot.common.utils.Event;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class FeedbackMessageActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "FeedbackMessageActivity";
    private int feedbackType;
    private String feedbackId;
    private FeedbackmessageActivityBinding binding;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCreate() {
        binding = FeedbackmessageActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {

        feedbackId = getIntent().getStringExtra("hdid");
        feedbackType = getIntent().getIntExtra("hdType", FeedBackParam.TYPE_APP);
        Log.i(TAG, " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 4 : " + feedbackType);

        binding.btnFeedbackSubmit.setEnabled(false);
        binding.etFeedBackContent.requestFocus();
        //获取帐号密码输入内容长度,设置光标位置
        binding.etFeedBackContent.setSelection(binding.etFeedBackContent.getText().toString().trim().length());

        /**
         * 对帐号密码输入框进行监听
         */
        binding.etFeedBackContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.d(TAG, "afterTextChanged------------" + binding.etFeedBackContent.length());
                binding.tvContentNumber.setText(binding.etFeedBackContent.length() + "");
                if (!"".equals(editable.toString().trim())) {
                    binding.btnFeedbackSubmit.setEnabled(true);
                    binding.btnFeedbackSubmit.setBackgroundDrawable(getResources().getDrawable(R.drawable.checked_btn_background));
                } else {
                    binding.btnFeedbackSubmit.setEnabled(false);
                    binding.btnFeedbackSubmit.setBackgroundDrawable(getResources().getDrawable(R.drawable.default_btn_background));
                }
            }
        });

        binding.ivFeedbackReturn.setOnClickListener(this);
        binding.btnFeedbackSubmit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if(id==R.id.iv_feedbackReturn){
            Intent_utils.backIntent(this);
        }else if (id==R.id.btn_feedbackSubmit){
            binding.lottieAnimationView.setVisibility(View.VISIBLE);
            String feedBackContent = binding.etFeedBackContent.getText().toString().trim();

            if (feedBackContent.length() == 0) {
                ToastUtil.show(this, getString(R.string.MY_Feedback_Hint), Toast.LENGTH_LONG);
                return;
            }
            Log.d(TAG, "feedBackContent.length------------" + binding.etFeedBackContent.length());
            setContent(feedBackContent);
        }
    }

    /**
     * @param str     :反馈内容
     * @param feedbackType  :反馈类型: 2:设备 ，7：其他
     */
    private void setContent(String str) {
        //获取
        if(TextUtils.isEmpty(feedbackId)){
            FeedBackParam param = FeedBackParam.newBuilder().option(FeedBackParam.ADD_FEEDBACK).type(feedbackType).content(str).build();
            AppContext.getAccountService().addFeedback(param);
        }else{
            FeedBackParam param = FeedBackParam.newBuilder().option(FeedBackParam.REPLY_FEEDBACK).feedbackId(feedbackId).type(feedbackType).content(str).build();
            AppContext.getAccountService().addFeedback(param);
        }


    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_ADD_FEEDBACK_MSG:
                binding.lottieAnimationView.setVisibility(View.GONE);
                if (event.isResult()) {
                    Log.i(TAG, "反馈添加成功 ");
                    ToastUtil.show(this, getString(R.string.MY_T_Feedback_Ok), Toast.LENGTH_SHORT);
                    finish();
                } else {
                    ToastUtil.show(this, event.getContent() + "", Toast.LENGTH_SHORT);
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
